import java.util.Random;
import java.util.Scanner;

public class NGuessNumber {

    // 生成随机数
    public static int[] generateAnswer() {
        //随机生成4个0-9之间的不重复的整型数
        int[] nums = new int[4];
        for (int i = 0; i < nums.length; i++) {
            nums[i] = new Random().nextInt(10);
            for (int j = 0; j < i; j++) {
                if (nums[i] == nums[j]) {
                    i--;
                    break;
                }
            }
        }
        return nums;
    }


    //接收玩家输入的4个数字并返回。
    public static int[] getPlayerInput() {
        Scanner scanner = new Scanner(System.in);
        int[] nums = new int[4];
        for (int i = 0; i < 4; i++) {
            nums[i] = scanner.nextInt();
        }
        return nums;
    }

    //比较玩家猜测和答案后返回提示信息。
    public static String compareGuessAnswer(int[] guess, int[] answer) {
        int a_num = 0;
        int b_num = 0;
        for (int i = 0; i < answer.length; i++) {
            for (int j=0; j < answer.length; j++){
                if(i!=j){
                    if(answer[i]==guess[j]){
                        b_num++;
                    }
                }else{
                    if(answer[i]==guess[j]){
                        a_num++;
                    }
                }

            }
        }
        System.out.println(a_num+"A"+b_num+"B");
        System.out.println("A表示存在且位置正确的数字个数，B表示存在且位置不正确的数字个数！");
        System.out.println("如：1A2B,表示你猜测的四个数字有3个是对的，有一个位置对，两个位置不对。\n");
        return a_num+"A"+b_num+"B";
    }


    public static boolean isWin(String hint) {
        if (hint.charAt(0) == '4') {
            return true;
        } else {
            return false;
        }
    }

    public static void main(String[] args) {
        String playAgain;
        do {
            int answer[] = generateAnswer();
            boolean isWin = false;
            while (!isWin) {
                int[] playerInput = getPlayerInput();
                String result = compareGuessAnswer(playerInput, answer);
                isWin = isWin(result);
                System.out.println(isWin ? "恭喜你猜对了！" : "很遗憾，你猜错了！");
            }
            //在玩一局
            System.out.println("是否再玩一局？(y/n)");
            Scanner scanner = new Scanner(System.in);
            playAgain = scanner.next();
        } while (playAgain.equals("y"));
    }
}
